<!-- src/views/ResetPassword.vue -->
<template>
  <div class="reset-password-container">
    <h2>重置密码</h2>
    <p>请输入您的新密码并确认以完成重置。</p>

    <el-form :model="form" :rules="rules" ref="formRef" label-width="0">
      <el-form-item prop="newPassword">
        <el-input
          v-model="form.newPassword"
          placeholder="新密码"
          type="password"
          show-password
        />
      </el-form-item>

      <el-form-item prop="confirmPassword">
        <el-input
          v-model="form.confirmPassword"
          placeholder="确认密码"
          type="password"
          show-password
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="handleReset" :loading="loading">重置密码</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { userApi } from '@/utils/api'

export default {
  name: 'ResetPassword',
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else if (value.length < 6) {
        callback(new Error('密码长度不能少于6位'))
      } else {
        callback()
      }
    }

    const validateConfirmPassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.form.newPassword) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }

    return {
      loading: false,
      form: {
        newPassword: '',
        confirmPassword: ''
      },
      rules: {
        newPassword: [{ validator: validatePassword, trigger: 'blur' }],
        confirmPassword: [{ validator: validateConfirmPassword, trigger: 'blur' }]
      },
      token: ''
    }
  },
  mounted() {
    // 从URL中获取token参数
    const urlParams = new URLSearchParams(window.location.search);
    this.token = urlParams.get('token');
    
    if (!this.token) {
      this.$message.error('缺少重置密码所需的token参数');
    }
  },
  methods: {
    async handleReset() {
      this.$refs.formRef.validate(async (valid) => {
        if (valid) {
          if (!this.token) {
            this.$message.error('缺少重置密码所需的token参数');
            return;
          }
          
          this.loading = true;
          try {
            const response = await userApi.resetPassword({
              token: this.token,
              new_password: this.form.newPassword
            });
            
            this.$message.success('密码重置成功！');
            // 可以跳转到登录页
            setTimeout(() => {
              this.$router.push('/login');
            }, 1500);
          } catch (error) {
            this.$message.error(error.response?.data?.detail || '重置密码失败');
          } finally {
            this.loading = false;
          }
        } else {
          console.log('验证失败');
        }
      });
    }
  }
}
</script>

<style scoped>
.reset-password-container {
  max-width: 400px;
  margin: 100px auto;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  background-color: #fff;
  text-align: center;
}

.reset-password-container h2 {
  color: #20a09f;
  margin-bottom: 15px;
}

.reset-password-container p {
  color: #666;
  margin-bottom: 20px;
}

.el-form-item {
  margin-bottom: 20px;
}

.el-button {
  width: 100%;
  background-color: #20a09f;
  border-color: #20a09f;
  color: white;
}

.el-button:hover {
  background-color: #1a8c85;
}
</style>